const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');

const app = getApp();
var canUseReachBottom = true;//触底函数控制变量
Page({
    data: {
        goodsList: [],
        page: 1,
        hasPrint: 1,
        noMore: true,
        y: 0,
    },
    getFootprintList() {
        let that = this;
        let token = wx.getStorageSync('token');
        util.request(api.FootprintList, {page: that.data.page, token: token}).then(function (res) {
            if (res.code == 0) {
                let count = res.data.length;
                if (count == 0) {
                    that.setData({
                        hasPrint: 0,
                        noMore: true
                    });
                } else {
                    that.setData({
                        goodsList: res.data,
                    });
                }
            }
            wx.hideLoading();
        });
    },
    onLoad: function (options) {
        this.getFootprintList();
    },
    deleteImage: function (e) {
        let that = this;
        let id = e.currentTarget.dataset.id;
        let token = wx.getStorageSync('token');
        wx.showModal({
            title: '提示',
            content: '您确定要删除么？',
            success: function(res) {
                if (res.confirm) {
                    util.request(api.FootprintDelete, {id: id, token: token}, 'POST').then(function (res){
                        if (res.code == 0) {
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                mask: true
                            });
                            that.getFootprintList();
                        } else {
                            util.showErrorToast(res.msg);
                        }
                    });
                }
            }
        })
    },
    toIndexPage: function (e) {
        wx.switchTab({
            url: '/pages/index/index'
        });
    },
    scrollToLower: function (e) {
        if(!canUseReachBottom) return;//如果触底函数不可用，则不调用网络请求数据
        let that = this;
        let page = that.data.page + 1;
        let no = that.data.noMore;
        let token = wx.getStorageSync('token');
        if (this.data.noMore) {
            canUseReachBottom = false;//触底函数关闭
            util.request(api.FootprintList, {page: page, token: token}).then(function (res){
                if (res.data.length > 0 && no) {
                    that.setData({
                        page: page,
                        goodsList: that.data.goodsList.concat(res.data)
                    })
                } else {
                    that.setData({
                        noMore: false
                    })
                }
                canUseReachBottom = true;//有新数据，触底函数开启，为下次触底调用做准备
            })
        }
    },
    onPullDownRefresh: function() {
        wx.showNavigationBarLoading();
        this.getFootprintList();
        wx.hideNavigationBarLoading();//完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },
})